package googlescrapper;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.IncorrectnessListener;
import com.gargoylesoftware.htmlunit.ScriptException;
import com.gargoylesoftware.htmlunit.SilentCssErrorHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HTMLParserListener;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.javascript.JavaScriptErrorListener;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.swing.JTable;
import javax.swing.JTextPane;
import org.apache.commons.logging.LogFactory;
import org.w3c.css.sac.CSSException;
import org.w3c.css.sac.CSSParseException;
import org.w3c.css.sac.ErrorHandler;

/**
 * Google keyword scrapper with proxy
 * @author Shaiful Islam (palash)
 * @email kuvic16@gmail.com
 */
public class AdSearching  implements Runnable {
    private Thread thread;
    private String keyword;
    private String threadName;
    private javax.swing.JTextPane consle;
    private javax.swing.JTable tblOutput;
    private Object[][] rows = null;
    private List<String> urlList = null;
    private List<Output> outputList = null;
    private String url;
    GoogleScrapper googleScrapper;
    

    public AdSearching(String _threadName, String _keyword, JTextPane _console, Object[][] _rows, List<Output> _output, JTable _tblOutput, String _url) {
        threadName = _threadName;
        keyword = _keyword;
        consle = _console;
        rows = _rows;
        urlList = new ArrayList<>();
        outputList = _output;
        if(outputList == null){
            outputList = new ArrayList<Output>();
        }
        tblOutput = _tblOutput;
        url = _url;
    }

    @Override
    public void run() {
        googleAdSearching(url, keyword);
    }

    private void googleAdSearching(String url, String keyword){
        try {
            URI uri = new URI(url);
            Output output = new Output();
            output.setRootDomain(uri.getHost());
            output.setActualDestination(url);
            output.setKeyword(keyword);
            output.setAdFlag("0");

            final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24);
            configureHtmlUnit(webClient);
            final HtmlPage page = webClient.getPage(url);
            DomNodeList<DomElement> d = page.getElementsByTagName("iframe");
            for (DomElement dd : d) {
                String id = dd.getAttribute("id");
                if (id.contains("google_ads_iframe")) {
                    output.setAdFlag("1");
                    break;
                }
            }
            webClient.closeAllWindows();
            this.getGoogleScrapper().addToOutputList(output);
            //this.getGoogleScrapper().updateOutputTables();
            //this.getGoogleScrapper().logIt("DONE\n");            
        } catch (Throwable t) {
            this.getGoogleScrapper().logIt("Error during google adSearching: " + t.getMessage() + "\n");
        }
    }

    public void configureHtmlUnit(WebClient webClient) {
        webClient.setIncorrectnessListener(new IncorrectnessListener() {

            @Override
            public void notify(String arg0, Object arg1) {
                // TODO Auto-generated method stub

            }
        });
        webClient.setCssErrorHandler(new ErrorHandler() {

            @Override
            public void warning(CSSParseException exception) throws CSSException {
                // TODO Auto-generated method stub

            }

            @Override
            public void fatalError(CSSParseException exception) throws CSSException {
                // TODO Auto-generated method stub

            }

            @Override
            public void error(CSSParseException exception) throws CSSException {
                // TODO Auto-generated method stub

            }
        });
        webClient.setJavaScriptErrorListener(new JavaScriptErrorListener() {

            @Override
            public void timeoutError(HtmlPage arg0, long arg1, long arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void scriptException(HtmlPage arg0, ScriptException arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void malformedScriptURL(HtmlPage arg0, String arg1, MalformedURLException arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void loadScriptError(HtmlPage arg0, URL arg1, Exception arg2) {
                // TODO Auto-generated method stub

            }
        });
        webClient.setHTMLParserListener(new HTMLParserListener() {

            @Override
            public void error(String string, URL url, String string1, int i, int i1, String string2) {

            }

            @Override
            public void warning(String string, URL url, String string1, int i, int i1, String string2) {

            }
        });

        webClient.setCssErrorHandler(new SilentCssErrorHandler());
        webClient.getCookieManager().setCookiesEnabled(false);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setPrintContentOnFailingStatusCode(false);
    }
    
    public void start() {
        thread = new Thread(this, threadName);
        thread.start();
    }
    
    public GoogleScrapper getGoogleScrapper() {
        return googleScrapper;
    }

    public void setGoogleScrapper(GoogleScrapper googleScrapper) {
        this.googleScrapper = googleScrapper;
    }
}
